﻿using Domain.Entity;
using DtoFw;
using DtoFw.Query;
using Repository;
using System;
using System.Collections.Generic;
using System.Text;
using Util;

namespace Service
{
    public class MainNumberService
    {
        private readonly MainSerialNumberRepository _mainSerialNumberRepository;
        public MainNumberService(MainSerialNumberRepository mainSerialNumberRepository)
        {
            this._mainSerialNumberRepository = mainSerialNumberRepository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public BaseResult Post(SerialNumberPostDto dto)
        {
            var entity = AutoMapperExtension.MapTo<SerialNumberPostDto, MainNumberEntity>(dto);
            entity.CreatorId = dto.UserId;
            entity.Creator = dto.UserName;
            if (_mainSerialNumberRepository.Insert(entity) > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("新增编号失败");
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public BaseResult Put(SerialNumberPutDto dto)
        {
            var entity = _mainSerialNumberRepository.Get(new BaseQuery() { Id = dto.Id });
            if (entity == null)
                return BaseResult.Failure("参数异常");
            entity = AutoMapperExtension.MapTo<SerialNumberPutDto, MainNumberEntity>(dto, entity);
            entity.ModifyerId = dto.UserId;
            entity.Modifyer = dto.UserName;
            if (_mainSerialNumberRepository.Save(entity)?.Id > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("更新编号序列失败");
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public BaseGenericResult<List<SerialNumberQueryDto>> Query()
        {
            var list = _mainSerialNumberRepository.Query(new BaseQuery())?.ToList();
            var dtoList = AutoMapperExtension.MapTo<MainNumberEntity, SerialNumberQueryDto>(list);
            return BaseGenericResult<List<SerialNumberQueryDto>>.Succeed(dtoList);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public BaseResult Delete(DeleteSingleDto query)
        {
            if (_mainSerialNumberRepository.Delete(new Domain.Base.BaseOperate() { Id = query.Id }) > 0)
                return BaseResult.Succeed();
            return BaseResult.Failure("删除失败");
        }
    }
}
